package com.dmall.graph.dfs.app;

import com.dmall.graph.Graph;

import java.util.ArrayList;

/**
 * @author: xiao1.wang
 * @date: 2022/6/19 14:42
 */
public class AllPairsPath1 {

    private Graph G;
    private SingleSourcePath1[] singleSourcePaths;

    public AllPairsPath1(Graph G) {
        this.G = G;
        this.singleSourcePaths = new SingleSourcePath1[G.V()];
        for (int i = 0; i < singleSourcePaths.length; i++) {
            singleSourcePaths[i] = new SingleSourcePath1(G, i);
        }
    }

    public boolean isConnected(int s, int t) {
        G.validateVertex(s);
        G.validateVertex(t);
        return this.singleSourcePaths[s].isConnectedTo(t);
    }

    public Iterable<Integer> path(int s, int t) {
        ArrayList<Integer> res = new ArrayList<>();
        if (!isConnected(s, t)) {
            return res;
        }
        return this.singleSourcePaths[s].path(t);
    }

    public static void main(String[] args) {
        String filename = AllPairsPath1.class.getClassLoader().getResource("g.txt").getPath();
        Graph g = new Graph(filename);
        AllPairsPath1 allPairsPath = new AllPairsPath1(g);
        System.out.println("0 -> 6 : " + allPairsPath.path(0, 6));
        System.out.println("0 -> 5 : " + allPairsPath.path(0, 5));
    }
}
